package com.zzsy.carosshopprojects.activitys.home.mainpage.menu.changeshifts;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;

import com.yatoooon.screenadaptation.ScreenAdapterTools;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.adapter.ChangeShiftsAdapter;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.bean.ChangeShiftsBean;
import com.zzsy.carosshopprojects.view.refreshview.RefreshView;
import com.zzsy.carosshopprojects.view.refreshview.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * 交接班
 */
public class ChangeShiftsFragment extends BaseFragment implements RefreshView.RefreshHelper {
    public static ChangeShiftsFragment newInstance() {
        ChangeShiftsFragment changeShiftsFragment = new ChangeShiftsFragment();
        return changeShiftsFragment;
    }

    @BindView(R.id.smartRefresh)
    RefreshView smartRefresh;
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.no_data_view)
    LinearLayout emptyView;
    private ChangeShiftsAdapter changeShiftsAdapter;
    private ChangeShiftsMode changeShiftsMode;
    private int page = 1;
    List<ChangeShiftsBean.ListBean> listData;
    private boolean isShowEmpty = true;
    private boolean isShowDialog = true;
    private int kind = 0;//0 下拉刷新 1 加载更多

    @Override
    protected int initLayout() {
        return R.layout.fh_changeshifts;
    }

    @Override
    protected void initView(View view) {
        smartRefresh.setRefreshHelper(this);
        initTitle(view, getResources().getString(R.string.changeShiftHistory), getActivity());
    }

    @Override
    protected void initData(Context mContext) {
        listData = new ArrayList<>();
        changeShiftsMode = getViewModelProvider().get(ChangeShiftsMode.class);
        changeShiftsAdapter = new ChangeShiftsAdapter(context, listData, 0);
        recycleView.setLayoutManager(new LinearLayoutManager(context));
        int space = ScreenAdapterTools.getInstance().loadCustomAttrValue(20);
        recycleView.addItemDecoration(new SpacesItemDecoration(space));
        recycleView.setAdapter(changeShiftsAdapter);
    }

    @Override
    protected void loadMethod() {
        showData();
    }

    /**
     * 获取列表数据
     */
    private void showData() {
        changeShiftsMode.getLiveData().observe(this, bean -> {
            listData.addAll(bean.getList());
            changeShiftsAdapter.setListData(listData);
            changeShiftsAdapter.notifyDataSetChanged();
            if (!isShowDialog) {
                switch (kind) {
                    case 0:
                        smartRefresh.finishRefresh();
                        break;
                    case 1:
                        smartRefresh.finishLoadMore();
                        if (bean.getList() != null && bean.getList().size() > 0)
                        {}
                        else
                            smartRefresh.finishLoadMoreWithNoMoreData();
                        break;
                }
            }
            setEmptyData();

        });
        changeShiftsMode.getData(getContext(), page + "", isShowDialog);
    }

    /**
     * 如果没用数据是否显示空View
     */
    private void setEmptyData() {
        if (isShowEmpty) {
            if (listData == null||listData.size() == 0) {
                emptyView.setVisibility(View.VISIBLE);
            } else {
                emptyView.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onRefresh() {
        listData.clear();
        changeShiftsAdapter.notifyDataSetChanged();
        kind = 0;
        page = 1;
        isShowEmpty = true;
        isShowDialog = false;
        changeShiftsMode.getData(getContext(), page + "", isShowDialog);
    }

    @Override
    public void onLoadMore() {
        isShowEmpty = false;
        isShowDialog = false;
        kind = 1;
        page++;
        changeShiftsMode.getData(getContext(), page + "", isShowDialog);
    }


}
